﻿using System.Web.Mvc;

namespace CodeContrib.Web.Mvc
{
	public class HttpStatusCodeResult : ViewResult
	{
		private readonly int _statusCode;

		public HttpStatusCodeResult(int statusCode)
		{
			_statusCode = statusCode;
		}

		public override void ExecuteResult(ControllerContext context)
		{
			TempData = context.Controller.TempData;

			if (ViewData == null)
			{
				ViewData = context.Controller.ViewData;
			}

			context.HttpContext.Response.Clear();
			context.HttpContext.Response.StatusCode = _statusCode;

			// Certain versions of IIS will sometimes use their own error page when
			// they detect a server error. Setting this property indicates that we
			// want it to try to render ASP.NET MVC's error page instead.
			context.HttpContext.Response.TrySkipIisCustomErrors = true;

			base.ExecuteResult(context);
		}
	}
}